package com.sudo.dao.admin.mapper;

import com.sudo.common.mapper.MyMapper;
import com.sudo.dao.admin.entity.SysAutoRole;
import com.sudo.dao.admin.model.bo.SysAutoRoleQueryBO;
import com.sudo.dao.admin.model.vo.SysAutoRoleVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface SysAutoRoleMapper extends MyMapper<SysAutoRole> {

    @Select("<script>" +
            " select sar.id,sar.username,sar.source,su.create_time ,sar.status, " +
            "   sr0.role_code ,sr0.role_name,sr0.status roleStatus, " +
            "   sr1.role_code setupRoleCode,sr1.role_name setupRoleName,sr1.status setupRoleStatus, " +
            "   sr2.role_code expiryRoleCode,sr2.role_name expiryRoleName,sr2.status expiryRoleStatus, " +
            "   sar.expiration_time expirationTime,sar.is_expired isExpired  " +
            "   from sys_auto_role sar  " +
            "   left join sys_user su on sar.username = su.username  " +
            "   left join sys_user_role sur on sar.username = sur.username " +
            "   left join sys_role sr0 on sur.role_code = sr0.role_code " +
            "   left join sys_role sr1 on sar.setup_role = sr1.role_code  " +
            "   left join sys_role sr2 on sar.expiry_role = sr2.role_code  " +
            "<where> " +
            "<if test=\"sysAutoRoleQueryBO.createTime != null \">" +
            "   <if test=\"sysAutoRoleQueryBO.createTime[0] != null and sysAutoRoleQueryBO.createTime[0] != '' \">" +
            "       and su.create_time <![CDATA[ >= ]]> STR_TO_DATE(#{sysAutoRoleQueryBO.createTime[0]},'%Y-%m-%d %H:%i:%s') " +
            "   </if>"+
            "   <if test=\"sysAutoRoleQueryBO.createTime[0] != null and sysAutoRoleQueryBO.createTime[1] != '' \">" +
            "       and su.create_time <![CDATA[ <= ]]> STR_TO_DATE(#{sysAutoRoleQueryBO.createTime[1]},'%Y-%m-%d %H:%i:%s') " +
            "   </if>"+
            "</if>"+
            "<if test=\"sysAutoRoleQueryBO.expirationTime != null\">" +
            "   <if test=\"sysAutoRoleQueryBO.createTime[0] != null and sysAutoRoleQueryBO.expirationTime[0] != '' \">" +
            "       and sar.expiration_time <![CDATA[ >= ]]> STR_TO_DATE(#{sysAutoRoleQueryBO.expirationTime[0]},'%Y-%m-%d %H:%i:%s') " +
            "   </if>"+
            "   <if test=\"sysAutoRoleQueryBO.createTime[0] != null and sysAutoRoleQueryBO.expirationTime[1] != '' \">" +
            "       and sar.expiration_time <![CDATA[ <= ]]> STR_TO_DATE(#{sysAutoRoleQueryBO.expirationTime[1]},'%Y-%m-%d %H:%i:%s') " +
            "   </if>"+
            "</if>"+
            "<if test=\"sysAutoRoleQueryBO.isExpired != null and sysAutoRoleQueryBO.isExpired != '' \">" +
            "   and sar.is_expired = #{sysAutoRoleQueryBO.isExpired} " +
            "</if>"+
            "<if test=\"sysAutoRoleQueryBO.username != null and sysAutoRoleQueryBO.username != '' \">" +
            "   and sar.username = #{sysAutoRoleQueryBO.username} " +
            "</if>"+
            "</where>" +
            " order by sar.id desc"+
            "</script>")
    List<SysAutoRoleVO> getAutoRoleInfo(@Param("sysAutoRoleQueryBO") SysAutoRoleQueryBO sysAutoRoleQueryBO);
}
